package com.yiyang.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiyang.mapper.BuildingMapper;
import com.yiyang.pojo.Building;
import com.yiyang.pojo.Result;
import com.yiyang.service.BuildingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
@Service
public class BuildingServiceImpl implements BuildingService {

    @Resource
    private BuildingMapper buildingMapper;

    @Override
    public Result<List<Building>> getBuilding(Integer currentPage, Integer pageSize, String name, String  height) {
        Page<Building> page = new Page<>(currentPage,pageSize);

        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),"name",name)
                .eq(StringUtils.isNotBlank(height),"height",height);

        this.buildingMapper.selectPage(page,wrapper);
        List<Building> list = page.getRecords();
        if (list.isEmpty()) {
            return Result.fail();
        }else{
            return Result.success(list,page.getTotal());
        }
    }

    @Override
    public Result<Void> addBuilding(Building building) {
        building.setCreateTime(new Date());
        building.setIsDelete(0);
        int count = this.buildingMapper.insert(building);
        if (count == 1){
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    @Override
    public Result<Void> updateBuilding(Building building) {
        int update = this.buildingMapper.updateById(building);
        if (update == 1){
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    @Override
    public Result<Void> deleteBuildingById(String  id) {
        int delete = this.buildingMapper.deleteById(id);
        if (delete == 1){
            return Result.success();
        } else {
            return Result.fail();
        }
    }
}
